﻿using Microsoft.Practices.Unity;
using MVVMtutorialAdvanced.DomainServices;
using Riba.MVVMSL;

namespace MVVMtutorialAdvanced.VMLocator
{
	//http://johnpapa.net/silverlight/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum/
	public class ViewModelLocator
	{
		private static IUnityContainer _container;

		public Indexer Find { get; private set; }

		public ViewModelLocator()
		{
			_container = new UnityContainer();

			if (ViewModelBase.IsInDesignTool)
				_container.RegisterType<IDomainServiceBook, DomainServiceBookMock>
					(new ContainerControlledLifetimeManager());  //Singleton
			else
				_container.RegisterType<IDomainServiceBook, DomainServiceBookLive>
					(new ContainerControlledLifetimeManager());  //Singleton

			Find = new Indexer { UnityContainer = _container};
		}
	}

	//Simple ViewModelLocator without Unity
	//public class ViewModelLocator
	//{
	//  private readonly IDomainServiceBook _service;

	//  public ViewModelLocator()
	//  {
	//    if (ViewModelBase.IsInDesignTool)
	//      _service = new DomainServiceBookMock();
	//    else
	//      _service = new DomainServiceBookLive();
	//  }

	//  public MainPage_VM MainPage_VM
	//  {
	//    get { return new MainPage_VM(); }
	//  }
	//  public Author_VM Author_VM
	//  {
	//    get { return new Author_VM(_service); }
	//  }
	//  public AuthorDetails_VM AuthorDetails_VM
	//  {
	//    get { return new AuthorDetails_VM(_service); }
	//  }
	//  public Books_VM Books_VM
	//  {
	//    get { return new Books_VM(_service); }
	//  }
	//  public BookAuthors_VM BookAuthors_VM
	//  {
	//    get { return new BookAuthors_VM(_service); }
	//  }
	//}
}
